package com.dzt.cloudreader.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;

import com.dzt.cloudreader.R;
import com.dzt.cloudreader.adapter.AndroidAdapter;
import com.dzt.cloudreader.base.LoadingFragment;
import com.dzt.cloudreader.model.GankIoDataBean;
import com.dzt.cloudreader.network.GankIoDataPresenter;
import com.dzt.cloudreader.network.GankIoDataView;
import com.dzt.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by M02323 on 2017/12/15.
 */

public class AndroidFragment extends LoadingFragment {
	@BindView(R.id.xrv_android)
	XRecyclerView recyclerView;
	private AndroidAdapter adapter;
	private List<GankIoDataBean.ResultsBean> lists = new ArrayList<>();
	private GankIoDataPresenter presenter;
	private static final String TYPE = "mType";
	private String mType = "Android";
	private int mPage = 1;

	public static AndroidFragment newInstance(String type) {
		AndroidFragment fragment = new AndroidFragment();
		Bundle args = new Bundle();
		args.putString(TYPE, type);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_android;
	}

	@Override
	protected void initData() {
		if (getArguments() != null) {
			mType = getArguments().getString(TYPE);
		}
		adapter = new AndroidAdapter(context, lists, R.layout.item_android);
		initRecyclerView();
		presenter = new GankIoDataPresenter(context);
		presenter.onCreate();
		presenter.attachView(dataView);
		loadAndroidData();
	}

	private void initRecyclerView() {
		recyclerView.setPullRefreshEnabled(false);
		recyclerView.clearHeader();
		recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
			@Override
			public void onRefresh() {
				mPage = 1;
				loadAndroidData();
			}

			@Override
			public void onLoadMore() {
				mPage++;
				loadAndroidData();
			}
		});
		recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
		// 需加，不然滑动不流畅
		recyclerView.setNestedScrollingEnabled(false);
		recyclerView.setHasFixedSize(false);
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		recyclerView.setAdapter(adapter);
	}

	private void loadAndroidData() {
		presenter.getGankIoData(mType, 20, mPage);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if(presenter != null){
			presenter.onDestroy();
		}
	}

	private GankIoDataView dataView = new GankIoDataView() {
		@Override
		public void onSuccess(GankIoDataBean gank) {
			showContentView();
			if (gank != null && gank.getResults() != null && gank.getResults().size() > 0) {
				recyclerView.refreshComplete();
				lists.addAll(gank.getResults());
				adapter.notifyDataSetChanged();
			} else {
				recyclerView.noMoreLoading();
			}
		}

		@Override
		public void onError(String result) {
			recyclerView.refreshComplete();
			// 注意：这里不能写成 mPage == 1，否则会一直显示错误页面
			if (adapter.getItemCount() == 0) {
				showError();
			}
			if (mPage > 1) {
				mPage--;
			}
		}
	};
}
